package org.abettor.nookinfo.novel;

import java.util.Random;

import org.abettor.nookinfo.R;
import org.abettor.nookinfo.ads.YoumiManager;
import org.abettor.nookinfo.persist.NovelHelper;
import org.abettor.nookinfo.persist.NovelHelper.LastRead;
import org.abettor.nookinfo.persist.NovelHelper.LocalChapter;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 本地文章内容界面
 * @author shawn
 *
 */
public class NovelLocalContent extends Activity
{
	private static final int REFRESHED = 1024;//文章更新已完成事件
	private static final int MENU_PREV = Menu.FIRST + 1;
	private static final int MENU_NEXT = MENU_PREV + 1;
	private static final int MENU_REFRESH = MENU_NEXT + 1;
	private static final int MENU_CLOSE = MENU_REFRESH + 1;
	private ActionType action = ActionType.refresh;
	private YoumiManager youmi = new YoumiManager(this);
	private NovelHelper novelHelper = new NovelHelper(this);
	private String novelName;
	private int chapter;
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			if(msg.what == REFRESHED)
			{
				//	获取章节正文
				LocalChapter localChapter = null;
				switch(action)
				{
				case refresh:
					localChapter = novelHelper.getLocalChapter(chapter);
					break;
				case prev:
					localChapter = novelHelper.getPrevLocalChapter(chapter);
					break;
				case next:
					localChapter = novelHelper.getNextLocalChapter(chapter);
					break;
				}

				if(localChapter != null)
				{
					TextView textView = (TextView) findViewById(R.id.novelContentText);
					textView.setText(localChapter.content.trim());
					chapter = localChapter.id;

					ScrollView scroll = (ScrollView) findViewById(R.id.novelContentScroll);
					scroll.scrollTo(0, 0);
	
					//	按照一定的概率显示广告
					Random rand = new Random(System.currentTimeMillis());
					int val = rand.nextInt(99);
					if(val < 20)
					{
						youmi.showYoumi(YoumiManager.Position.middle, Color.WHITE, Color.BLACK, 0xAA);
					}
	
					//	记录最近访问历史
					LastRead lastRead = new LastRead();
					lastRead.category = getString(R.string.novel_local_content_category);
					lastRead.id = chapter;
					lastRead.local = true;
					lastRead.novel = novelName;
					novelHelper.setLastRead(lastRead);
				}
				else
				{
					Builder builder = new Builder(NovelLocalContent.this);
					builder.setTitle(R.string.novel_local_content_fail_title);
					builder.setMessage(R.string.novel_local_content_fail_content);
					builder.setPositiveButton(R.string.novel_local_content_fail_btn_ok, new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialoginterface, int i)
						{
							dialoginterface.dismiss();
						}
					});
					builder.show();
				}
			}

			super.handleMessage(msg);
		}
	};
	private Runnable worker = new Runnable()
	{
		@Override
		public void run()
		{
			Message msg = new Message();
			msg.what = REFRESHED;
			handler.sendMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.novel_local_content);

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		novelName = extras.getString(NovelLocalChapter.EXTRA_NOVEL_NAME);
		chapter = extras.getInt(NovelLocalChapter.EXTRA_CHAPTER_ID);
		String label = getString(R.string.novel_local_content_label).replace("REPL", novelName);
		setTitle(label);

		action = ActionType.refresh;
		handler.postDelayed(worker, 200);
	}

	/**
	 * 创建Menu菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_PREV, MENU_PREV, getString(R.string.global_menu_prev)).setIcon(R.drawable.global_menu_prev);
		menu.add(0, MENU_NEXT, MENU_NEXT, getString(R.string.global_menu_next)).setIcon(R.drawable.global_menu_next);
		menu.add(0, MENU_REFRESH, MENU_REFRESH, getString(R.string.global_menu_refresh)).setIcon(R.drawable.global_menu_refresh);
		menu.add(0, MENU_CLOSE, MENU_CLOSE, getString(R.string.global_menu_close)).setIcon(R.drawable.global_menu_close);

		return result;
	}

	/**
	 * 处理菜单事件
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
		case MENU_PREV:
			onPrevClicked(null);
			break;
		case MENU_NEXT:
			onNextClicked(null);
			break;
		case MENU_REFRESH:
			onRefreshClicked(null);
			break;
		case MENU_CLOSE:
			onRetrunClicked(null);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	/**
	 * “刷新”按钮事件方法
	 * @param v
	 */
	public void onRefreshClicked(View v)
	{
		action = ActionType.refresh;
		handler.post(worker);
	}

	/**
	 * “返回”按钮事件方法
	 * @param v
	 */
	public void onRetrunClicked(View v)
	{
		finish();
	}

	/**
	 * “上一页”按钮事件方法
	 * @param v
	 */
	public void onPrevClicked(View v)
	{
		action = ActionType.prev;
		handler.post(worker);
	}

	/**
	 * “下一页”按钮事件方法
	 * @param v
	 */
	public void onNextClicked(View v)
	{
		action = ActionType.next;
		handler.post(worker);
	}

	//	动作类型枚举
	private enum ActionType
	{
		refresh,
		prev,
		next;
	}
}
